package mina.forwhat.config;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.LineDelimiter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import mina.forwhat.service.Demo1ServerHandler;


@Configuration
public class MinaConfig {
	private static Logger logger = Logger.getLogger(MinaConfig.class);
    
    @Bean
    public LoggingFilter loggingFilter() {
        return new LoggingFilter();
    }
    @Bean
    public IoHandler ioHandler() {
        return new Demo1ServerHandler();
    }
    @Bean
    public InetSocketAddress inetSocketAddress() {
        return new InetSocketAddress(9123);
    }
    
    @Bean
    public IoAcceptor ioAcceptor() throws Exception {
    IoAcceptor acceptor=new NioSocketAcceptor();
    acceptor.getFilterChain().addLast( "logger", loggingFilter() );
        acceptor.getFilterChain().addLast( "codec",
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                        .forName("UTF-8"), LineDelimiter.WINDOWS.getValue(),
                        LineDelimiter.WINDOWS.getValue())));//因为传byte[]所以去掉
        acceptor.setHandler(ioHandler());


        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );


        acceptor.bind(inetSocketAddress());
        logger.info("服务端启动成功...     端口号为：" + inetSocketAddress().getPort());
        return acceptor;
    }
   
}